<?php

namespace App\Models\WorkOrder;

use App\Models\BaseModel;
use App\Models\Traits\KfSoftDeletes;
use App\Models\Traits\KfUid;

class WorkOrderUserRoleModel extends BaseModel
{
    use KfSoftDeletes;

    protected $table = 'work_order_user_role';

    protected $fillable = [
        'name',
        'feishuId',
        'mobile',
        'role',
        'userId',
        'problemPid',
        'problemChildId',
        'isDeleted',
    ];

    const ROLE_TYPE_1 = 1;
    const ROLE_TYPE_2 = 2;
    const ROLE_TYPE_3 = 3;
    const ROLE_TYPE_4 = 4;
    const ROLE_TYPE_5 = 5;
    const ROLE_TYPE_6 = 6;
    const ROLE_TYPE_7 = 7;

    const ROLE_TYPE_TEXT = [
        self::ROLE_TYPE_1 => '客服',
        self::ROLE_TYPE_2 => '品控负责人',
        self::ROLE_TYPE_3 => '仓配负责人',
        self::ROLE_TYPE_4 => '产品负责人',
        self::ROLE_TYPE_5 => '设备软装负责人',
        self::ROLE_TYPE_6 => '产品运营部',
        self::ROLE_TYPE_7 => '外卖负责人',
    ];

    public function scopeGetUsersByRole($query, $role, int $problemPid, int $problemChildId)
    {
        return $query->where('role', $role)
                     ->where('problemPid', $problemPid)
                     ->where(function ($q) use ($problemChildId) {
                         $q->where('problemChildId', $problemChildId)->orWhere('problemChildId', 0);
                     })
                     ->select(['userId', 'name'])
                     ->get()
                     ->map(function ($item) {
                         return [
                             'value' => (int)$item->userId,
                             'text'  => $item->name,
                         ];
                     })
                     ->toArray();
    }

}
